package com.kapcb.schedule;

import com.kapcb.schedule.client.XxlJobAdminHttpClient;
import com.kapcb.schedule.properties.XxlJobProperties;
import com.xxl.job.core.executor.impl.XxlJobSpringExecutor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * <a>Title: XxlJobAutoConfiguration </a>
 * <a>Author: kapcb <a>
 * <a>Description: XxlJobAutoConfiguration <a>
 *
 * @author Kpacb
 * @version 1.0
 * @date 2024/5/30 20:22
 * @since 1.0
 */
@Slf4j
@Configuration
@EnableConfigurationProperties(XxlJobProperties.class)
public class XxlJobAutoConfiguration {

    private XxlJobProperties xxlJobProperties;

    @Autowired
    public void setXxlJobProperties(XxlJobProperties xxlJobProperties) {
        this.xxlJobProperties = xxlJobProperties;
    }

    @Bean("xxlJobSpringExecutor")
    @ConditionalOnMissingBean(value = XxlJobSpringExecutor.class)
    public XxlJobSpringExecutor xxlJobSpringExecutor() {
        if (log.isDebugEnabled()) {
            log.debug("begin load XxlJobSpringExecutor into IOC ......");
        }
        XxlJobSpringExecutor xxlJobSpringExecutor = new XxlJobSpringExecutor();
        xxlJobSpringExecutor.setAdminAddresses(xxlJobProperties.getAdminAddresses());
        xxlJobSpringExecutor.setAppname(xxlJobProperties.getAppName());
        xxlJobSpringExecutor.setAddress(xxlJobProperties.getAddress());
        xxlJobSpringExecutor.setIp(xxlJobProperties.getIp());
        xxlJobSpringExecutor.setPort(xxlJobProperties.getPort());
        xxlJobSpringExecutor.setAccessToken(xxlJobProperties.getAccessToken());
        xxlJobSpringExecutor.setLogPath(xxlJobProperties.getLogPath());
        xxlJobSpringExecutor.setLogRetentionDays(xxlJobProperties.getLogRetentionDays());
        return xxlJobSpringExecutor;
    }

    @Bean("xxlJobAdminHttpClient")
    public XxlJobAdminHttpClient xxlJobAdminHttpClient() {
        if (log.isDebugEnabled()) {
            log.debug("begin load XxlJobAdminHttpClient into IOC ......");
        }
        return new XxlJobAdminHttpClient(xxlJobProperties);
    }

}
